package com.wnm.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.wnm.dao.GoodsDao;
import com.wnm.pojo.Goods;
import com.wnm.pojo.PageBean;
import com.wnm.service.GoodsService;
import com.wnm.util.DBUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	private GoodsDao goodsDao;

	@Override
	public List<Goods> findNewGoods() {
		List<Goods> newGoods = goodsDao.findNewGoods();
		return newGoods;
	}

	@Override
	public List<Goods> findHotGoods() {
		List<Goods> hotGoods = goodsDao.findHotGoods();
		return hotGoods;
	}

	@Override
	public PageBean<Goods> findPageByCid(int cid, int currentPage) {
		PageBean<Goods> pageBean = new PageBean();
		List<Goods> list = goodsDao.findPageByCidAndCurrertPage(cid, (currentPage - 1) * 12);
		pageBean.setCurrentPage(currentPage);
		pageBean.setList(list);
		int count = goodsDao.findCountByCid(cid);
		pageBean.setCount(count);
		pageBean.setPages(count % 12 == 0 ? count / 12 : count / 12 + 1);
		pageBean.setPageSize(12);
		return pageBean;
	}

	@Override
	public List<Goods> findGoodsBySalesnums() {
		List<Goods> saleGoods = goodsDao.findGoodsBySalesnums();
		return saleGoods;
	}

	@Override
	public Goods goodsDetail(int gid) {
		Goods goods=goodsDao.findGoodsByGid(gid);
		return goods;
	}

	@Override
	public PageBean<Goods> findAllGoods(int currentPage) {
		PageBean<Goods> pageBean = new PageBean();
		List<Goods> list = goodsDao.findAllGoods(currentPage);
		pageBean.setCurrentPage(currentPage);
		pageBean.setList(list);
		int count = goodsDao.findCount();
		pageBean.setCount(count);
		pageBean.setPages(count % 12 == 0 ? count / 12 : count / 12 + 1);
		pageBean.setPageSize(12);
		return pageBean;	
	}

	@Override
	public PageBean<Goods> findPageByCondition(String condition, int currentPage) {
		PageBean<Goods> pageBean = new PageBean();
		List<Goods> list = goodsDao.findPageByCondition(condition, (currentPage - 1) * 12);
		pageBean.setCurrentPage(currentPage);
		pageBean.setList(list);
		int count = goodsDao.findCountByCondition(condition);
		pageBean.setCount(count);
		pageBean.setPages(count % 12 == 0 ? count / 12 : count / 12 + 1);
		pageBean.setPageSize(12);
		return pageBean;
	}


}
